package online.inote.common.core;

import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;

import lombok.Data;

/**
 * <p>
 * Desc: 分页
 * </p>
 *
 * @author XQF SUI
 * @created 2017年12月4日 上午11:03:54
 * @version 1.0
 */
@Data
public class Page<T> {

	// 查询结果 0:成功
	private int code = 0;
	// 状态信息
	private String msg = "";
	
    // 当前页
    private int page = 1;
    // 每页显示条数
    private int rows = 10;
    // 开始条数
    @JSONField(serialize = false)
    private int start = 0;
    // 总页数
    private long totalPage;
    // 总数
    private long records;
    // 是否有下一页
    private boolean hasNext;
    // 是否有上一页
    private boolean hasPrevious;
    // 获取的数据
    private List<T> dataList;
    
    public Page() {
        super();
    }
    
    public Page(int page, int rows) {
    	this();
        this.page = page > 0 ? page : 1;
        this.rows = rows > 0 ? rows : 30;
        this.start = (this.page - 1) * this.rows;  
    }
    
    public void setRecords(long records) {
        this.records = records;
        this.totalPage = (long) Math.ceil((double) records / this.rows);
        this.page = this.totalPage > this.page ? this.page : (int) this.totalPage;
        this.hasNext = this.totalPage > this.page;
        this.hasPrevious = this.page > 1;
    }
    
	@JSONField(serialize = false)
    public int getESPage() {
    	return this.page > 0 ? this.page - 1 : 0;
    }
}
